/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.web;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.merak.core.model.parameter.Parameter;

public class WebParameterForm extends Parameter implements WebParameter {

	//~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	//~ Constructors /////////////////////////////////////////////////////////////
	//****************************************************************************
	protected WebParameterForm(String name,HttpServletRequest request) {

		super(
			name,                      // Parameter Name : the form input name
			request.getParameter(name) // Parameter Value: the form input value
		);

	}
	//****************************************************************************
	protected WebParameterForm(FileItem item) {

		super(
			item.getFieldName(), // Parameter Name : the form input name
			item.getString()     // Parameter Value: the form input value
		);

	}

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	@Override
	public boolean isUpload() {
		return false;
	}
	//****************************************************************************
	@Override
	public String getFileName() {
		return "";
	}
	//****************************************************************************
	@Override
	public boolean hasFileType(String type) {
		return false;
	}
	//****************************************************************************
	@Override
	public long getLength() {
		return super.getValue().length();
	}
	//****************************************************************************
	@Override
	public String getFileType() {
		return "";
	}
	//****************************************************************************
	@Override
	public File getFile() throws Exception {
		return null;
	}
	//****************************************************************************
	@Override
	public File getFile(File file) throws Exception {
		return file;
	}
	//****************************************************************************
	@Override
	public byte[] getBytes() {
		return super.getValue().getBytes();
	}
	//****************************************************************************
	@Override
	public InputStream getInputStream() throws IOException {
		return new ByteArrayInputStream(this.getBytes());
	}
	//****************************************************************************
	@Override
	public void writeToOutputStrem(OutputStream out) throws IOException {
		out.write( this.getBytes() );
	}
	//****************************************************************************
	@Override
	public boolean equals(Object other) {

		if (other==null) return false;

		return (other instanceof WebParameterForm)
		       && ((WebParameterForm)other).hasValue(this.getValue());

	}
	//****************************************************************************

}
